const log = require('../../util/log')
const config = require('../../util/config')
const logger = require('log4js').getLogger()

async function checkLoginJd(obj) {
  const {
    Network,
    Page,
    DOM,
    Runtime,
    Emulation
  } = this.client
  obj = obj || {}
  obj.times = (obj.times || 0) + 1
  if (obj.step !== 0) {
    logger.debug('[' + __filename + ':' + __line + ']', 'checking login jd')
    obj.step = 0
  }
  try {
    Page.navigate({
      url: 'https://media.jd.com/'
    })
    return step1.call(this, obj)
  } catch (err) {
    log(err)
  }
}

async function step1(obj) {
  try {
    const {
      Network,
      Page,
      DOM,
      Runtime,
      Emulation
    } = this.client
    obj.times++;
    if (obj.times > config.chromeMaxTimes) {
      return new Promise((resolve, reject) => {
        reject('超时')
      })
    }
    if (obj.step !== 1) {
      logger.debug('[' + __filename + ':' + __line + ']', 'checking login jd step 1')
      obj.step = 1
    }
    var {
      result
    } = await Runtime.evaluate({
      expression: 'location.href'
    })
    if (result.value == 'https://media.jd.com/') {
      return step2.call(this, obj)
    }
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        step1.call(this, obj).then(result => {
          resolve(result)
        }, result => {
          reject(result)
        })
      }, 1000)
    })
  } catch (err) {
    log(err)
  }
}

async function step2(obj) {
  try {
    const {
      Network,
      Page,
      DOM,
      Runtime,
      Emulation
    } = this.client;
    obj.times++;
    if (obj.times > config.chromeMaxTimes) {
      return new Promise((resolve, reject) => {
        reject('超时')
      })
    }
    if (obj.step !== 2) {
      logger.debug('[' + __filename + ':' + __line + ']', 'checking login jd step 2')
      obj.step = 2
    }
    var {
      result
    } = await Runtime.evaluate({
      expression: 'document.querySelectorAll("#userPin")'
    })
    var cnt = result.description.substring(9, result.description.length - 1)
    if (cnt == 1) {
      var {
        result
      } = await Runtime.evaluate({
        expression: 'document.querySelectorAll("#userPin")[0].innerText'
      })
      if (result.value != '') {
        return new Promise((resolve, reject) => {
          resolve(true)
        })
      }
    }
    var {
      result
    } = await Runtime.evaluate({
      expression: 'document.querySelectorAll("iframe")'
    })
    var cnt = result.description.substring(9, result.description.length - 1)
    if (cnt == 0) {
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          step2.call(this, obj).then(result => {
            resolve(result)
          }, result => {
            reject(result)
          })
        }, 1000)
      })
    }
    if (cnt > 1) {
      return new Promise((resolve, reject) => {
        reject('iframe多于1个')
      })
    }
    var {
      result
    } = await Runtime.evaluate({
      expression: 'document.querySelectorAll("iframe")[0].contentDocument.querySelectorAll("#o-authcode")'
    })
    var cnt = result.description.substring(9, result.description.length - 1)
    if (cnt == 1) {
      var {
        result
      } = await Runtime.evaluate({
        expression: 'document.querySelectorAll("iframe")[0].contentDocument.querySelectorAll("#o-authcode")[0].className'
      })
      if (result.value.indexOf('hide') == -1) {
        return new Promise((resolve, reject) => {
          log('要输验证码')
          resolve(false)
        })
      }
    }
    var {
      result
    } = await Runtime.evaluate({
      expression: 'document.querySelectorAll("iframe")[0].contentDocument.querySelectorAll("#loginname")'
    })
    var cnt = result.description.substring(9, result.description.length - 1)
    if (cnt == 0) {
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          step2.call(this, obj).then(result => {
            resolve(result)
          }, result => {
            reject(result)
          })
        }, 1000)
      })
    }
    return new Promise((resolve, reject) => {
      resolve(false)
    })
  } catch (err) {
    log(err)
    return new Promise((resolve, reject) => {
      resolve(false)
    })
  }
}

module.exports = checkLoginJd